using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;


public class ItemToolTip : MonoBehaviour
{
   public Text itemName;
   public Text itemInfo;
   public Text itemPrice;
   private RectTransform transform;
   public int type;

   public Text skillName;
   public Text skillInfo;
   public InventoryItem targetItem;

   private void OnEnable()
   {
      updatePosition();
   }

   private void Awake()
   {
      transform = GetComponent<RectTransform>();
      if (type == 1)
      {
         updatePosition();
      }
   }

   private void Update()
   {
      if (type == 0)
      {
         updatePosition();
      }
   }

   public void SetUpTooltip(ItemData_SO itemData)
   {
      itemName.gameObject.SetActive(true);
      itemInfo.gameObject.SetActive(true);
      skillInfo.gameObject.SetActive(false);
      skillName.gameObject.SetActive(false);
      itemPrice.gameObject.SetActive(false);
      itemName.text = itemData.itemName;
      itemInfo.text = itemData.description;

   }
   public void SetUpTooltipForShop(ItemData_SO itemData)
   {
      itemName.gameObject.SetActive(true);
      itemInfo.gameObject.SetActive(true);
      skillInfo.gameObject.SetActive(false);
      skillName.gameObject.SetActive(false);
      itemName.text = itemData.itemName;
      itemInfo.text = itemData.description;
      itemPrice.text = "单价:  " + itemData.price;
      itemPrice.gameObject.SetActive(true);
   }

   public void SetUpTooltip(Skill_SO skill)
   {
      skillInfo.gameObject.SetActive(true);
      skillName.gameObject.SetActive(true);
      itemName.gameObject.SetActive(false);  
      itemInfo.gameObject.SetActive(false);
      skillName.text = skill.skillName;
      skillInfo.text = skill.skillDescription;
   }

   public void updatePosition()
   {
      Vector3 mousePos = Input.mousePosition;
      Vector3[] corners = new Vector3[4];
      transform.GetWorldCorners(corners);
      float width = corners[3].x - corners[0].x;
      float height = corners[1].y - corners[0].y;
      
      if (mousePos.y < height)
      {
         transform.position = mousePos + Vector3.up * height * 0.6f;
      }
      else if (Screen.width - mousePos.x < width)
      {
         transform.position = mousePos + Vector3.left * width * 0.6f;
      }
      else
      {
         transform.position = mousePos + Vector3.right * width * 0.6f;
      }
   }

   public void Buy()
   {
      if (GameManager.Instance.moneyData.MoneyAmount >= targetItem.itemData.price)
      {
         DOTween.To(()=>GameManager.Instance.moneyData.MoneyAmount,x=>GameManager.Instance.moneyData.MoneyAmount=x,GameManager.Instance.moneyData.MoneyAmount-targetItem.itemData.price, 0.5f);
         var bagdata = InventoryManager.Instance.bagData;
         var shopdata = InventoryManager.Instance.shopData;
         bagdata.AddItem(targetItem.itemData, targetItem.amount);
         shopdata.AddItem(targetItem.itemData,-1);
         InventoryManager.Instance.shopUI.RefreshUI();
         InventoryManager.Instance.bagUI.RefreshUI();
         gameObject.SetActive(false);
      }
      else
      {
         gameObject.SetActive(false);
      }
   }
}
